import java.io.File;
import java.io.FileInputStream;
import javax.activation.MimetypesFileTypeMap;

/*
 * Servlet is verantwoordelijk voor het al het werk dat een client veroorzaakt.
 *
 * we doen alleen head en get request
 *
 * creates: Response
 * uses: Request
 * returns: Response
 */
class Servlet
{
	private Control control;
	private Request request;
	private Response response;
	private int id;

	public Servlet(Control control, int id)
	{
		System.out.println("Servlet, Servlet()");
		this.control = control;
		this.id = id;
		
		this.response = new Response();
	}

	public void doGet()
	{
		System.out.println("Servlet, doGet()");
		
		String fileName = request.getFilename().equals("")? "index.html" : request.getFilename();

		File file = new File(control.getContentBase() + request.getFolder() + fileName);
		String mimetype = new MimetypesFileTypeMap().getContentType(file);

		control.log("Servlet: trying to open file '" + control.getContentBase() + request.getFolder() + file.getName() + "' (" + mimetype + ").", id);

		if (!file.isDirectory())
		{
			try
			{
				FileInputStream fis = new FileInputStream(file);
				//BufferedInputStream bis = new BufferedInputStream(fis);
				byte[] b = new byte[(int) file.length()];
				fis.read(b);
				doHead(200, "OK");
				response.setBYTES(b);
				response.addHeader("Content-Type", mimetype);
				response.addHeader("Content-Length", ""+b.length);

				fis.close();
				//file = null;
			}
			catch (Exception e)
			{
				control.log("Servlet: requested file '" + fileName + "' not found.", id);
				doHead(404, "Not Found");
				response.setBYTES("We're sorry, the requested file could not be found.".getBytes());
			}
			finally 
			{

			}
		}
		else
		{
			doHead(403, "Forbidden");
			response.setBYTES("The request is not authorized.".getBytes());
		}
	}

	public void doHead(int code, String reason)
	{
		System.out.println("Servlet, doHead(int code, String reason)");
		response.setCODE(code);
		response.setREASON(reason);
	}

	public void doPost()
	{
		System.out.println("Servlet, doPost()");
	}

	public void doPut()
	{
		System.out.println("Servlet, doPut()");
	}

	public void doDelete()
	{
		System.out.println("Servlet, doDelete()");
	}

	public void doOptions()
	{
		System.out.println("Servlet, doOptions()");
	}

	public Response service(Request request)
	{
		System.out.println("Servlet, service(Request request)");
		this.request = request;

		if (request.getMETHOD().equals("GET"))
		{
			doGet();
		}
		else
		{
			doHead(501, "Not Implemented");
			response.setBYTES("It is currently only possible to do a GET request.".getBytes());
		}

		return response;
	}

	public void finalize() throws Throwable
	{
		System.out.println("Servlet, finalize()");
		response.clear();
		request.clear();
	}

	public String toString() {
		System.out.println("Servlet, toString()");
		return "[Servlet]";
	}
}